package com.gues.ddc_app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gues.ddc_app.entity.Menu;
import com.gues.ddc_app.entity.ResultData;
import com.gues.ddc_app.entity.User;
import com.gues.ddc_app.service.impl.MenuServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huHang
 * @since 2023-02-12
 */
@RestController
@CrossOrigin//跨域
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuServiceImpl menuService;
    @RequestMapping("/deleteById")
    public Object deleteById(Integer id){
        Menu menu=new Menu();
        menu.setMenuId(id);
        return menuService.removeById(menu);
    }
    @RequestMapping("/updateMenu")
    public Object updateMenu(Integer menuId,Integer shopId,String menuName,Integer price){
        Menu menu=new Menu();
        menu.setMenuId(menuId);
        menu.setMenuName(menuName);
        menu.setShopId(shopId);
        menu.setPrice(price);
        if(menuService.saveOrUpdate(menu)){
            return menuService.getById(menu);
        }
        return null;
    }
    @RequestMapping("/addMenu")
    public Object addMenu(Integer shopId,String menuName,Integer price,@RequestParam("file") MultipartFile file){
        Menu menu=new Menu();
        menu.setMenuName(menuName);
        menu.setShopId(shopId);
        menu.setPrice(price);
        String oldName=file.getOriginalFilename();
        String suffix=oldName.substring(oldName.lastIndexOf("."));
        String newName= UUID.randomUUID()+suffix;
        try {
            file.transferTo(new File("D:\\IDEA\\img",newName));
        }catch (IOException e){
            System.out.println("上传异常"+e);
        }
        menu.setImage(newName);
        if(menuService.saveOrUpdate(menu)){
            return menuService.getById(menu);
        }
        else{
            return null;
        }
    }
    @RequestMapping("/upMenu")
    public Object  upMenu(Menu menu){
        if (menuService.updateById(menu)){
            return menu;
        }else
            return null;
    }
    @RequestMapping("/update")
    public boolean update(Menu menu){
        return menuService.updateById(menu);
    }
    @RequestMapping("/queryAll")
    public ResultData<Menu> queryAll(Integer page, Integer limit, @RequestParam(defaultValue = "") String keyword){
        QueryWrapper<Menu> wrapper=new QueryWrapper<>();
        wrapper.like("menu_name",keyword);
        Page<Menu> menuPage = new Page<>(page,limit);
        menuService.page(menuPage,wrapper);
        int a=(int)menuPage.getTotal();
        ResultData<Menu> userResultData = new ResultData<>((Integer)a,menuPage.getRecords());
        return userResultData;
    }
    @RequestMapping("/queryAll1")
    public Object queryAll1(@RequestParam(defaultValue = "") String keyword,Integer id){
        QueryWrapper<Menu> wrapper=new QueryWrapper<>();
        wrapper.like("menu_name",keyword);
        wrapper.eq("m.shop_id",id);
        return menuService.getList(wrapper);
    }
    @RequestMapping("/deleteByIds")
    public boolean deleteByIds(Integer[] ids){
        return menuService.removeBatchByIds(Arrays.asList(ids));
    }
}
